//
//  WPHomePageViewModel.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/18.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPHomePageViewModel.h"
#import "WPNewsPreheatModel.h"

@implementation WPHomePageViewModel
+ (void)updateHomePageDataSourceWithPageNumber:(NSNumber *)pageNumber andWithSuccessBlock:(void (^)(NSArray *, NSInteger))success andWithFailureBlock:(void (^)(NSString *))failure {
    NSDictionary * params = @{@"pageNumber":pageNumber,
                              @"pageSize":@(10)};
    [NetWorkingManager postWithUrl:@"commoditytrade/list.jhtml" requestParams:[NetWorkingManager paramsByAppendingUserInfo:params] SuccessHandler:^(id responseObject) {
        NSMutableArray * resultArr = [NSMutableArray arrayWithCapacity:0];
        if (responseObject[@"resultType"] && [responseObject[@"resultType"] integerValue]) {
            if ([responseObject[@"resultMsg"] isKindOfClass:[NSArray class]]) {
                for (NSDictionary * dict in responseObject[@"resultMsg"]) {
                    WPNewsPreheatModel * model = [WPNewsPreheatModel new];
                    [model setValuesForKeysWithDictionary:dict];
                    [resultArr addObject:model];
                }
            }
            success(resultArr, responseObject[@"totalpages"]?[responseObject[@"totalpages"] integerValue]:1);
        } else {
            failure(responseObject[@"resultMsg"]?responseObject[@"resultMsg"]:@"获取首页数据失败");
        }
    } failureHandler:^(NSError *error) {
        failure(error.localizedDescription);
    }];
}
@end
